package service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import constant.Constant;
import dao.CommentDao;
import entity.Comment;
import exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.CommentService;
import vo.CommentVo;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentDao commentDao;

    //加载评论
    @Override
    public PageInfo<CommentVo> loadComment(CommentVo c) {
        PageHelper.startPage(c.getPageNo(), Constant.PAGE_SIZE);
        List<CommentVo> list = commentDao.selectAllComment(c);//所有评论
        for (CommentVo i : list) {
            int praiseCount = commentDao.queryPraiseCountByCommentId(i.getId());//赞数
            i.setPraise_count(praiseCount);
        }
        return new PageInfo<CommentVo>(list);
    }

    //添加评论
    @Override
    public Boolean insertComment(Comment comment) throws ServiceException {
        int result = commentDao.insertComment(comment);
        return result>0?true:false;
    }


    //获取总赞数
    @Override
    public PageInfo<CommentVo> getPraCount(CommentVo c) {
        return null;
    }


    //加载待审核评论
    @Override
    public PageInfo<CommentVo> CMloadCommeommentVo(CommentVo c) {
        PageHelper.startPage(c.getPageNo(), Constant.PAGE_SIZE);
        List<CommentVo> list = commentDao.selectAllCommentByStatus(c);
        return new PageInfo<CommentVo>(list);
    }

    //审核评论，更新评论状态,审核评论，通过：0;禁用：1
    @Override
    public void updateCommentStatus(CommentVo c) {
        commentDao.updateCommentStatusById(c);

    }

    //加载待管理评论
    @Override
    public PageInfo<CommentVo> findCommentByChapterId(CommentVo c) {
        PageHelper.startPage(c.getPageNo(), Constant.PAGE_SIZE);
        List<CommentVo> list = commentDao.findCommentByChapterId(c);

        for (CommentVo i : list) {
            int praiseCount = commentDao.queryPraiseCountByCommentId(i.getId());//赞数
            i.setPraise_count(praiseCount);
        }
        return new PageInfo<CommentVo>(list);

    }

}
